package com.adjaisd.scarecrow.testJava;

import com.adjaisd.scarecrow.common.config.MqttConfig;
import com.adjaisd.scarecrow.common.utils.mqtt.EmqClient;
import com.adjaisd.scarecrow.common.utils.mqtt.MqttUtils;
import com.adjaisd.scarecrow.entity.Device;
import com.adjaisd.scarecrow.entity.DeviceGps;
import com.adjaisd.scarecrow.entity.DeviceStatus;
import com.adjaisd.scarecrow.service.*;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@SpringBootTest
public class testMqtt {

    @Autowired
    EmqClient emqClient;

    @Autowired
    MqttConfig mqttConfig;

    @Autowired
    RedisService redisService;

    @Test
    void testGetMqttToken() {    // 请求mqttToken
        Map<String, String> map = MqttUtils.getToken(mqttConfig, redisService);
        System.out.println(map.get("username"));
        System.out.println(map.get("password"));
    }

    @Test
    void testConnect() {
        Map<String, String> map = MqttUtils.getToken(mqttConfig, redisService);
        String username = map.get("username");
        String password = map.get("password");
        String clientId = "monitor." + UUID.randomUUID();
        System.out.println("MqttUtils连接emq");
        try {
            MqttClient mqttClient = new MqttClient(mqttConfig.getBrokerUrl(), clientId);
            MqttConnectOptions options = new MqttConnectOptions();
            options.setUserName(username);
            options.setPassword(password.toCharArray());
            options.setMaxInflight(500);
            mqttClient.connect(options);

            MqttMessage msg = new MqttMessage(map.toString().getBytes());
            mqttClient.getTopic("test/getToken").publish(msg);
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    @Test
    void testConnectPublish() {  // 连接并发布
        emqClient.connect();
        Map<String, Object> map = new HashMap<>();
        map.put("username", "adsfb");
        map.put("msg", "测试msg");
        emqClient.publish("test/getToken", map.toString(), 1);
    }

    @Test
    void testUpdateDeviceGps() {
        String username = "raspberry";
        String password = "123456";
        String clientId = "monitor." + UUID.randomUUID();
        System.out.println("MqttUtils连接emq");
        HashMap<String, String> map = new HashMap<>();
        map.put("number", "10001");
        map.put("lat", "1.3333");
        map.put("lon", "23.9909");
        try {
            MqttClient mqttClient = new MqttClient(mqttConfig.getBrokerUrl(), clientId);
            MqttConnectOptions options = new MqttConnectOptions();
            options.setUserName(username);
            options.setPassword(password.toCharArray());
            options.setMaxInflight(500);
            mqttClient.connect(options);

            MqttMessage msg = new MqttMessage(map.toString().getBytes());
            mqttClient.getTopic("device-gps").publish(msg);
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    @Autowired
    DeviceService deviceService;

    @Autowired
    DeviceGpsService deviceGpsService;

    @Autowired
    DeviceStatusService deviceStatusService;

    @Test
    void testCode() {
        Map<String, Object> map = new HashMap<>();
        map.put("number", "10001");
        map.put("lat", "28.59732");
        map.put("lon", "28.59732");
        System.out.println(deviceGpsService.analysis("device/gps", map));
    }

}
